import { http } from '@/utils/request';
import type { HttpResponse } from '@/utils/request';

export interface LoginPayload {
  account: string;
  password: string;
}

export interface ChangePasswordPayload {
  oldPassword: string;
  newPassword: string;
}

export interface UserInfo {
  distributorId?: string;
  distributorName?: string;
  phone?: string;
  headImg?: string;
  nickName?: string;
}

export interface UpdateHeaderPayload {
  nickName?: string | null;
  headImg?: string | null;
}

/**
 * 经销商登录
 */
export async function distributorLogin(payload: LoginPayload): Promise<string> {
  const response = await http.post<string>('/user/login', payload);
  return (response.data as string) || '';
}

/**
 * 获取用户详情
 */
export async function fetchUserInfo(): Promise<UserInfo | null> {
  const response = await http.post<UserInfo>('/user/getUserInfo');
  return (response.data as UserInfo) || null;
}

/**
 * 退出登录
 */
export async function signOut(): Promise<HttpResponse<null>> {
  return http.post<null>('/user/logout');
}

/**
 * 修改密码
 */
export async function changePassword(payload: ChangePasswordPayload): Promise<HttpResponse<null>> {
  // TODO: 后端文档将修改密码接口写为 /user/login，后续如有调整需要同步修改路径
  return http.post<null>('/user/updatePassword', payload);
}

/**
 * 更新用户头像昵称
 */
export async function updateHeader(payload: UpdateHeaderPayload): Promise<HttpResponse<null>> {
  return http.post<null>('/user/updateHeader', payload);
}

